package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Code07FileStatus {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {

        // 在conf对象中添加配置信息
        Configuration conf = new Configuration();
//        conf.set("dfs.replication","2");

        System.setProperty("HADOOP_USER_NAME", "root");
        URI uri = new URI("hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(uri, conf);

        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/data/ddd.txt"));
        Path path = fileStatus.getPath();
        String owner = fileStatus.getOwner();
        long len = fileStatus.getLen();
        // 判断是否为文件
        boolean file = fileStatus.isFile();

        System.out.println("owner:"+owner);
        System.out.println("len:"+len);
        System.out.println("isFile:"+file);

        // 获取对应文件所有的Block块位置信息
        BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, len);
        for (BlockLocation fileBlockLocation : fileBlockLocations) {
            String[] hosts = fileBlockLocation.getHosts();
            for (String host : hosts) {
                System.out.println(path+"block块Hosts:"+host);
            }
        }

        fileSystem.close();
    }
}
